package parser.main;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.io.File;

import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JScrollPane;

public class Main {

	private JFrame frmCorvsXmlParser;
	private JTextField textField;
	private JTextArea consoleLog;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frmCorvsXmlParser.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public Main() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCorvsXmlParser = new JFrame();
		frmCorvsXmlParser.setResizable(false);
		frmCorvsXmlParser.setTitle("Corv's XML Parser");
		frmCorvsXmlParser.setBounds(100, 100, 890, 745);
		frmCorvsXmlParser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmCorvsXmlParser.setJMenuBar(menuBar);
		
		JMenu mnArchivo = new JMenu("Archivo");
		menuBar.add(mnArchivo);
		
		JMenuItem mntmSalir = new JMenuItem("Salir");
		mntmSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		mnArchivo.add(mntmSalir);
		frmCorvsXmlParser.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 884, 696);
		frmCorvsXmlParser.getContentPane().add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 0, 884, 199);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(248, 42, 245, 20);
		panel_1.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Examinar");
		btnNewButton.setBounds(503, 41, 89, 23);
		panel_1.add(btnNewButton);
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.setBounds(364, 99, 89, 23);
		panel_1.add(btnAceptar);
		btnAceptar.setEnabled(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser folderChooser = new JFileChooser("c:\\");
				folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = folderChooser.showOpenDialog(null);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					File folder = folderChooser.getSelectedFile();
					File [] filesList = folder.listFiles();
					for(int i=0;i<filesList.length;i++){
						if(filesList[i].isFile()){
							if(filesList[i].getName().endsWith(".xml") || filesList[i].getName().endsWith(".XML")){
								getConsoleLog().append("File "+i+" "+filesList[i]+"\n");
							}
						}
					}										
				}				
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 371, 884, 325);
		panel.add(scrollPane);
		
		consoleLog = new JTextArea();
		scrollPane.setViewportView(consoleLog);
	}
	public JTextArea getConsoleLog() {
		return consoleLog;
	}
}
